from django.shortcuts import render, HttpResponse, redirect
from app01 import models
import json
# Create your views here.

def business(request):
	v = models.Business.objects.all()
	v2 = models.Business.objects.all().values('id','caption')
	v3 = models.Business.objects.all().values_list('id','caption')
	# print(v2)
	# print(v3)
	return render(request,'business.html',{'v':v,'v2':v2,'v3':v3})

def	host(request):
	if request.method == 'POST':
		h = request.POST.get('hostname')
		ip = request.POST.get('ip')
		p = request.POST.get('port')
		b_id = request.POST.get('b_id')
		models.Host.objects.create(hostname=h,
								   ip=ip,
								   port=p,
								   b_id=b_id)
	v = models.Host.objects.all()
	v2 = models.Business.objects.all()
	return render(request, 'host.html', {'v': v, 'v2': v2})

def test_ajax(request,wf):
	ret = {'status':True, 'err':None}
	# print(request.path_info)
	try:
		h = request.POST.get('hostname')
		ip = request.POST.get('ip')
		p = request.POST.get('port')
		b_id = request.POST.get('b_id')
		if h and len(h) >= 5:
			if request.path_info == '/test_ajax/':
				models.Host.objects.create(hostname=h,
										   ip=ip,
										   port=p,
										   b_id=b_id)
			elif request.path_info == '/test_ajax_edit/':
				# print('hello')
				hid = request.POST.get('hid')
				models.Host.objects.filter(nid=hid).update(hostname=h,
										   ip=ip,
										   port=p,
										   b_id=b_id)
		else:
			ret['err'] = "too short!"
			ret['status'] = False
	except Exception as e:
		ret['err'] = e
		ret['status'] = False
	return HttpResponse(json.dumps(ret))

def app(request):
	if request.method == "POST":
		host_list = request.POST.getlist('host_list')  # 是一个列表,nid
		app_name = request.POST.get('app_name')
		obj = models.Application.objects.create(name=app_name) # 重要
		obj.r.add(*host_list)
	app_list = models.Application.objects.all()
	host_list = models.Host.objects.all()
	return render(request,'app.html',{'app_list':app_list,'host_list':host_list})

def ajax_add_app(request):
	ret = {'status':True,'err':None,'data':None}
	try:
		app_name = request.POST.get('app_name')
		host_list = request.POST.getlist('host_list')
		obj = models.Application.objects.create(name=app_name)
		obj.r.add(*host_list)
	except Exception as e:
		ret['status'] = False
		ret['err'] = e
	return HttpResponse(json.dumps(ret))

def app_edit(request):
	aid = request.POST.get('aid')  #host_id aid
	host_id_list = request.POST.getlist('host_id')
	obj = models.Application.objects.filter(id=aid).first()
	obj.r.set(host_id_list)

	return HttpResponse('haha')

def del_host(request):
	if request.method == "POST":
		try:
			ret = {'status':True, 'err_msg':None}
			hid = request.POST.get('hid')
			models.Host.objects.filter(nid=hid).delete()
		except Exception as e:
			ret['status'] = False
			ret['err_msg'] = e
		return HttpResponse(json.dumps(ret))
# def host(request):
# 	v = models.Host.objects.all()
# 	v2 = models.Business.objects.all()
# 	# v2 = models.Host.objects.all().values('nid','hostname','b__caption')
# 	# print(v2)
# 	return render(request, 'host.html',{'v':v,'v2':v2})

